package com.crm.customers.service.impl;

import java.util.HashMap;
import java.util.List;

import com.crm.util.CommonUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.customers.dao.ContactsDynamicInfoDao;
import com.crm.customers.dao.CustomerDao;
import com.crm.customers.model.ContactsDynamicInfo;
import com.crm.customers.model.CustomerInfo;
import com.crm.customers.service.ContactsDynamicInfoService;
import com.crm.util.Pager;

@Service("contactsDynamicInfoService")
public class ContactsDynamicInfoServiceImpl implements ContactsDynamicInfoService {

	@Autowired
	private ContactsDynamicInfoDao contactsDynamicInfoDao;
	
	@Autowired
	private CustomerDao customerDao;
	
	@Override
	public ContactsDynamicInfo saveContactsDynamicInfo(
			ContactsDynamicInfo cinfo) {
		
		if(cinfo.getId()!=null&&cinfo.getId()>0){
			contactsDynamicInfoDao.updateContactsDynamicInfoState(cinfo);
			Long cusid = cinfo.getCusid();
			CustomerInfo ci = customerDao.getCustomerInfoById(cusid);
			ci.setUpdatedate("");
			customerDao.updateCustomerInfo(ci);
		}else{
			contactsDynamicInfoDao.insertContactsDynamicInfo(cinfo);
			if (cinfo.getState() == 2) {
				Long cusid = cinfo.getCusid();
				CustomerInfo ci = customerDao.getCustomerInfoById(cusid);
				ci.setUpdatedate("");
				customerDao.updateCustomerInfo(ci);
			}
		}
		return cinfo;
	}

	@Override
	public Pager getContactsDynamicInfo(ContactsDynamicInfo cinfo, int pageNo,
			int pageSize) {
		cinfo.setStartsize((pageNo-1)*pageSize);
		cinfo.setEndsize(pageSize);
		List<ContactsDynamicInfo>  list = contactsDynamicInfoDao.getContactsDynamicInfo(cinfo);
	    Pager  pager = new Pager();
		pager.setPageNo(pageNo);
		pager.setPageSize(pageSize);
		pager.setResultList(list);
	    return pager;
	}

	@Override
	public Pager getContactsDynamicInfoList(ContactsDynamicInfo cdinfo,
			int pageNo, int pageSize) {
		cdinfo.setStartsize((pageNo-1)*pageSize);
		cdinfo.setEndsize(pageSize);
		List<ContactsDynamicInfo>   list = contactsDynamicInfoDao.getContactsDynamicInfoList(cdinfo);
		  Pager  pager = new Pager();
			pager.setPageNo(pageNo);
			pager.setPageSize(pageSize);
			pager.setResultList(list);
		return pager;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public HashMap getCustomerEnteringCount(Long uid) {
		ContactsDynamicInfo cdinfotouch=new ContactsDynamicInfo();
		ContactsDynamicInfo cdinfoentering=new ContactsDynamicInfo();
		CustomerInfo cinfo=new CustomerInfo();
		
		cinfo.setCreateuser(uid);
		cinfo.setCreatedate(CommonUtil.getDateAll(""));
		
		cdinfotouch.setCreateuser(uid);//创建人
		cdinfotouch.setCreatedate(CommonUtil.getDateAll(""));//创建时间
		cdinfotouch.setState(1);
		
		cdinfoentering.setCreateuser(uid);//创建人
		cdinfoentering.setStime(CommonUtil.getDateAll(""));//预约时间
		cdinfoentering.setState(0);//状态
		
		
		int typeinCount=customerDao.getCustomerTypeinCount(cinfo);//录入
		int touchCount=contactsDynamicInfoDao.getCustomerTouchCount(cdinfotouch);//联系
		int enteringCount=contactsDynamicInfoDao.getCustomerEnteringCount(cdinfoentering);//预约
		HashMap map=new HashMap();
		map.put("1", typeinCount);
		map.put("2", touchCount);
		map.put("3", enteringCount);
		return map;
	}

	@Override
	public int getContactsDynamicInfoByTime(ContactsDynamicInfo cdinfo) {
		return contactsDynamicInfoDao.getContactsDynamicInfoByTime(cdinfo);
	}

	@Override
	public List<ContactsDynamicInfo> getAllcdinfolist(
			ContactsDynamicInfo cdinfo, int pageNo, int pageSize) {
		cdinfo.setStartsize((pageNo-1)*pageSize);
		cdinfo.setEndsize(pageSize);
		return contactsDynamicInfoDao.getAllcdinfoList(cdinfo);
	}

}
